import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/components/home/Home'
import Login from '@/components/Login'
import Register from '@/components/Register'
import Header from '@/components/common/Header'
import BlogDetail from '@/components/blog/BlogDetail'
import BlogEdit from '@/components/blog/BlogEdit'
import Notes from '@/components/home/Notes'
import Blogs from '@/components/blog/Blogs'
import Center from '@/components/personal/Center'
import UserInfo from '../components/personal/UserInfo'
import BlogManagemen from '../components/personal/BlogManagemen'
import Author from '../components/author/Author'
Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'Home',
      component: Home
    },
    {
      path: '/home',
      name: 'Home',
      component: Home
    },
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/register',
      name: 'Register',
      component: Register
    },
    {
      path: '/header',
      name: 'Header',
      component: Header
    },
    {
      path: '/detail/:blogId',
      name: 'BlogDetail',
      component: BlogDetail
    },
    {
      path: '/edit',
      name: 'BlogEdit',
      meta: {
        requireAuth: true
      },
      component: BlogEdit
    },
    {
      path: '/notes',
      name: 'Notes',
      meta: {
        requireAuth: true
      },
      component: Notes
    },
    {
      path: '/typeBlogs/:typeId',
      name: 'Blogs',
      component: Blogs
    },
    {
      path: '/personal',
      name: 'Center',
      component: Center,
      redirect: 'UserInfo',
      children: [{
        path: '/userInfo',
        component: UserInfo,
        name: 'UserInfo'
      }, {
        path: '/BlogManagemen',
        component: BlogManagemen,
        name: 'BlogManagemen'
      }]
    },
    {
      path: '/Author/:author',
      name: 'Author',
      component: Author
    }
  ]
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login' || to.path === '/register' || to.path === '/') {
    next()
  } else {
    const token = localStorage.getItem('token')
    if (token === 'null' || token === '') {
      next('/login')
    } else {
      next()
    }
  }
  /* if (to.matched.some(record => record.meta.requiresAuth)) {     // 哪些需要验证
    if (!sessionStorage.getItem("token")) {                      // token存在条件
      next({
        path: '/',                                               // 验证失败要跳转的页面
        query: {
          redirect: to.fullPath                                 // 要传的参数
        }
      })
    } else {
      next()
    }
  } else {
    next()                                                       // 确保一定要调用 next()
  } */
})
const originalPush = Router.prototype.push
Router.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}
export default router
